import { MenuItem } from '@/types/menu';

/**
 * 菜单权限检查工具
 */
export class MenuPermissionChecker {
    private menuData: MenuItem[] = [];

    /**
     * 设置菜单数据
     */
    setMenuData(menuData: MenuItem[]) {
        this.menuData = menuData;
    }

    /**
     * 检查路由是否有权限访问
     * @param path 路由路径
     * @returns 是否有权限
     */
    hasPermission(path: string): boolean {
        if (!this.menuData || this.menuData.length === 0) {
            // 如果没有菜单数据，默认允许访问
            console.log('菜单数据为空，允许访问:', path);
            return true;
        }

        const hasPermission = this.checkPathInMenu(this.menuData, path);
        console.log('权限检查:', path, '结果:', hasPermission);
        return hasPermission;
    }

    /**
     * 递归检查路径是否在菜单中且已启用
     * @param menus 菜单数据
     * @param path 要检查的路径
     * @returns 是否有权限
     */
    private checkPathInMenu(menus: MenuItem[], path: string): boolean {
        for (const menu of menus) {
            // 检查当前菜单项
            if (path.includes(menu.menuCode)) {
                // 检查菜单是否启用且显示
                const isEnabled = menu.status === 1;
                console.log('找到匹配菜单:', menu.menuName, '启用状态:', isEnabled, 'status:', menu.status, 'isDisplay:', menu.isDisplay);
                return isEnabled;
            }

            // 递归检查子菜单
            if (menu.children && menu.children.length > 0) {
                if (this.checkPathInMenu(menu.children, path)) {
                    return true;
                }
            }
        }
        return false;
    }

    /**
     * 获取菜单项信息
     * @param path 路由路径
     * @returns 菜单项信息
     */
    getMenuInfo(path: string): MenuItem | null {
        if (!this.menuData || this.menuData.length === 0) {
            return null;
        }

        return this.findMenuByPath(this.menuData, path);
    }

    /**
     * 递归查找菜单项
     * @param menus 菜单数据
     * @param path 要查找的路径
     * @returns 菜单项信息
     */
    private findMenuByPath(menus: MenuItem[], path: string): MenuItem | null {
        for (const menu of menus) {
            if (menu.url === path) {
                return menu;
            }

            if (menu.children && menu.children.length > 0) {
                const found = this.findMenuByPath(menu.children, path);
                if (found) {
                    return found;
                }
            }
        }
        return null;
    }

    /**
     * 获取所有可访问的路径
     * @returns 路径数组
     */
    getAllAccessiblePaths(): string[] {
        if (!this.menuData || this.menuData.length === 0) {
            return [];
        }

        const paths: string[] = [];
        this.collectPaths(this.menuData, paths);
        return paths;
    }

    /**
     * 递归收集所有可访问的路径
     * @param menus 菜单数据
     * @param paths 路径数组
     */
    private collectPaths(menus: MenuItem[], paths: string[]): void {
        for (const menu of menus) {
            if (menu.status === 1 && menu.isDisplay === 1 && menu.url) {
                paths.push(menu.url);
            }

            if (menu.children && menu.children.length > 0) {
                this.collectPaths(menu.children, paths);
            }
        }
    }
}

// 创建全局实例
export const menuPermissionChecker = new MenuPermissionChecker();
